<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter :title="title" :width="900" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
    <DelayriskInsurantTable ref="insurantTableRef"></DelayriskInsurantTable>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { delayriskFormSchema } from '../data/cardserver.data';
import DelayriskInsurantTable from '../components/DelayriskInsurantTable.vue';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { getUserGoodsDetail } from '@apps/ant-bus/src/views/business/userManage/userGoods/data/api';
import { saveOrUpdate } from '@apps/ant-bus/src/views/business/userManage/cardserverRecord/data/api';
// 声明Emits
const emit = defineEmits(['success', 'register']);
//计算标题
const title = ref('');
const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  labelWidth: 150,
  labelCol: { span: 4 },
  schemas: delayriskFormSchema,
  showActionButtonGroup: false,
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  resetFields();
  setDrawerProps({ confirmLoading: false });
  let record = data.record;
  title.value = record.serverName;
  let userGoodsId = record.userGoodsId;
  let cardserverId = record.cardserverId;
  let result = await getUserGoodsDetail({
    userGoodsId: userGoodsId,
    handlerName: 'appUserGoodsGetHandler',
  });
  let newData = {
    cardserverId: cardserverId,
    userGoodsId: userGoodsId,
    userName: result.userName,
    identityType: result.identityType,
    identityNo: result.identityNo,
  };
  setFieldsValue({
    ...newData,
  });
});

const { createMessage } = useMessage();
const { error } = createMessage;
const insurantTableRef = ref<{ getDataSource: () => any }>();

/**
 * 获取被保障人数据
 */
function getInsurantData() {
  let newInsurantArray: any = [];
  if (insurantTableRef.value) {
    let insurantArray = insurantTableRef.value.getDataSource();
    let length = insurantArray.length;
    for (let i = 0; i < length; i++) {
      let insurant = insurantArray[i];
      let newInsurant = {
        userName: insurant.userName,
        identityType: insurant.identityType,
        identityNo: insurant.identityNo,
        birthDate: insurant.birthDate,
        sex: insurant.sex,
      };
      newInsurantArray.push(newInsurant);
    }
  }
  return newInsurantArray;
}

async function handleSubmit() {
  try {
    let insurantArray = getInsurantData();
    let length = insurantArray.length;
    setFieldsValue({
      ...{ numValue: length },
    });
    if (length == 0) {
      error('请录入被保障人信息');
      return;
    }
    const values = await validate();
    let insurantData = JSON.stringify(insurantArray);
    values['insurantData'] = insurantData;
    values['handlerName'] = 'cardServerDelayriskUseHandler';
    console.info(values);
    setDrawerProps({ confirmLoading: true });
    await saveOrUpdate(values, false);
    closeDrawer();
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
